package com.qihang.web.controller;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap;
import org.springframework.web.servlet.view.RedirectView;

import com.qihang.common.Constants;
import com.qihang.core.domain.UserEntity;
import com.qihang.core.service.UserService;
import com.qihang.util.AES;
import com.qihang.util.MD5Encoder;
import com.qihang.util.RequestUtil;


/**
 * @author limn
 *
 */
@Controller
@RequestMapping("/sys")
public class LoginController {

	@Resource(name = "userService")
	private UserService userService;
	
	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public String login(HttpServletRequest request) throws Exception {
		HttpSession session = request.getSession(true);
		Cookie[] cookies = request.getCookies();
		UserEntity user = new UserEntity();
		
		if(cookies!=null){
	    	for (Cookie cookie : cookies) {
	    		String name = cookie.getName();
				if(name.equals("remember")){
					request.setAttribute("remember",cookie.getValue());
				}else if(name.equals("username")){
					request.setAttribute("username",cookie.getValue());
					user.setUsername(cookie.getValue());
				}else if(name.equals("password")){
					request.setAttribute("passwd",cookie.getValue());
					user.setPassword(MD5Encoder.encode(cookie.getValue()));
				}
	    	}
//	    	boolean exist = userService.isExist(user);
//	    	if(exist){
//	    		session.invalidate();
//	    		session = request.getSession();
//	    		session.setAttribute("user", user);
//				return Constants.SysIndex.FRONTFINDPWD_REDIRECT;
//			}
		}
		return Constants.SysIndex.SYSLOGIN;
	}
	
	@RequestMapping(value ="/login", method = RequestMethod.POST) 
	public String login(String username, String password, RedirectAttributesModelMap modelMap,
			HttpServletRequest request,HttpServletResponse response) throws Exception{
		
		String retrieveSavedRequest = RequestUtil.retrieveSavedRequest(request);
		password = AES.Decrypt(password, request.getSession().getId());
		HttpSession session = request.getSession(true);
		UserEntity user = new UserEntity();
		user.setUsername(username);
		user.setPassword(MD5Encoder.encode(password));
		
		boolean exist = userService.isExist(user);
		if(!exist){
			modelMap.addFlashAttribute("message","用户名或密码错误!");
			return Constants.indexCommon.SYSLOGIN_REDIRECT;
		}
		session.invalidate();
		session = request.getSession();
		session.setAttribute("user", user);
		
		String remember = request.getParameter("remember");
		if("1".equals(remember)){
			Cookie usernameCookie = new Cookie("username", username);
        	usernameCookie.setMaxAge(604800);
        	response.addCookie(usernameCookie);
        	Cookie passwordCookie = new Cookie("password", password);
        	passwordCookie.setMaxAge(604800);
        	response.addCookie(passwordCookie);
        	Cookie rememberCookie = new Cookie("remember", remember);
        	rememberCookie.setMaxAge(604800);
        	response.addCookie(rememberCookie);
		}else{//清除密码
			Cookie[] cookies = request.getCookies();
			for (Cookie cookie : cookies) {
				String name = cookie.getName();
				if(name.equals("remember")){//不记住密码
					Cookie rememberCookie = new Cookie("remember", "0");
					rememberCookie.setMaxAge(0);
					response.addCookie(rememberCookie);
				}else if(name.equals("username")){//用户名设置为空
					Cookie usernameCookie = new Cookie("username", "0");
					usernameCookie.setMaxAge(0);
					response.addCookie(usernameCookie);
				}else if(name.equals("password")){//密码设置为空
					Cookie passwordCookie = new Cookie("password", "0");
					passwordCookie.setMaxAge(0);
					response.addCookie(passwordCookie);
				}
			}
		}
		
		return "redirect:" + retrieveSavedRequest;//跳转至访问页面 
//		return Constants.SysIndex.FRONTFINDPWD_REDIRECT;
	}
	
	@RequestMapping("/logout")
	public String loginOut(ModelMap modelMap, HttpSession session) {
		session.setAttribute("user", null);
		return Constants.SysIndex.SYSLOGIN;

	}
}
